package com.mail.server.controller;

import com.mail.dubbo.api.UmsAdminRoleRelationApi;
import com.mail.model.db.UmsAdmin;
import com.mail.model.db.UmsRole;
import com.mail.model.dto.AdminPasswordDto;
import com.mail.model.vo.InfoVo;
import com.mail.model.vo.MyPage;
import com.mail.model.vo.PageResult;
import com.mail.server.common.R;
import com.mail.server.service.UmsAdminService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 后台用户表 前端控制器
 * @author 吕永乐
 * @date 2022/06/21 10:15
 **/
@RestController
@RequestMapping("/admin")
public class UmsAdminController {

	@Autowired
	private UmsAdminService umsAdminService;


	/**
	 * 登录以后返回token
	 * 接口地址 /admin/login
	 * @return
	 */
	@PostMapping("/login")
	private R<Object> login(@RequestBody UmsAdmin umsAdmin){
		return umsAdminService.login(umsAdmin);
	}

	/**
	 * 获取当前登录用户信息
	 * 接口地址 /admin/info
	 * @return
	 */
	@GetMapping("/info")
	private R<InfoVo> info(){
		InfoVo infoVo = umsAdminService.getInfo();
		return R.success(infoVo);
	}



	/**
	 * 根据用户名或姓名分页获取用户列表 (分页查询)
	 * 接口地址:/admin/list
	 *
	 * @param pageNum 当前页
	 * @param pageSize 每页展示条数
	 * @param keyword 搜索框关键词
	 * @return
	 */
	@GetMapping("/list")
	public R<MyPage<UmsAdmin>> list(@RequestParam(defaultValue = "1") Long pageNum,
						  @RequestParam(defaultValue = "10") Long pageSize,
						  String keyword){
		MyPage<UmsAdmin> myPage = umsAdminService.list(pageNum, pageSize, keyword);
		return R.success(myPage, "操作成功");
	}

	/**
	 * 用户注册
	 * 接口地址:/admin/register
	 * @param umsAdmin
	 * @return
	 */
	@PostMapping("/register")
	public R<UmsAdmin> register(@RequestBody UmsAdmin umsAdmin){
		return umsAdminService.register(umsAdmin);
	}

	/**
	 * 修改账号状态
	 * 接口地址:/admin/updateStatus/{id}
	 * @param id
	 * @return
	 */
	@PostMapping("/updateStatus/{id}")
	public R<Object> updateStatus(@PathVariable("id") Integer id,
								@RequestParam Map<String, Object> map){
		Long userId = Long.valueOf(id);
		Integer status = Integer.parseInt(String.valueOf(map.get("status")));
		return umsAdminService.updateStatus(userId, status);
	}

	/**
	 * 修改指定用户信息
	 * 接口地址:/admin/update/{id}
	 * @return
	 */
	@PostMapping("/update/{id}")
	public R<String> update(@PathVariable("id") Integer id,
								  @RequestBody UmsAdmin umsAdmin){
		return umsAdminService.update(umsAdmin);
	}

	/**
	 * 登出功能
	 * 接口地址:/admin/logout
	 * @return
	 */
	@PostMapping("/logout")
	public R<String> logout(){
		return R.success(null, "操作成功!");
	}

	/**
	 * 刷新token
	 * 接口地址:/admin/refreshToken
	 * @return
	 */
	@GetMapping("/refreshToken")
	public R<Map<String, Object>> refreshToken(){
		Map<String, Object> map = umsAdminService.refreshToken();
		return R.success(map, "操作成功");
	}

	/**
	 * 获取指定用户的角色
	 * 接口地址:/admin/role/{adminId}
	 * @param adminId
	 * @return
	 */
	@GetMapping("/role/{id}")
	public R<List<UmsRole>> getRolesInfo(@PathVariable("id") Long adminId){
		List<UmsRole> umsRoles = umsAdminService.getRolesInfo(adminId);
		return R.success(umsRoles, "操作成功!");
	}

	/**
	 * 给用户分配角色
	 * 接口地址:/admin/role/update
	 * @param adminId 用户id
	 * @param roleIds 分配的角色id数组
	 * @return
	 */
	@PostMapping("/role/update")
	public R<Integer> updateAdminRole(@RequestParam("adminId") Long adminId,
							 @RequestParam("roleIds") List<Long> roleIds){
		return umsAdminService.updateAdminRole(adminId, roleIds);
	}

	/**
	 * 删除指定用户信息
	 * 接口地址:/admin/delete/{id}
	 * @param adminId
	 * @return
	 */
	@PostMapping("/delete/{id}")
	public R<Integer> delete(@PathVariable("id") Long adminId){
		return umsAdminService.delete(adminId);
	}

	/**
	 * 获取指定用户信息
	 * 接口地址:/admin/{id}
	 * @return
	 */
	@GetMapping("/{id}")
	public R<UmsAdmin> userInfo(@PathVariable("id") Long id){
		UmsAdmin umsAdmin = umsAdminService.getUserInfo(id);
		return R.success(umsAdmin, "操作成功!");
	}

	/**
	 * 修改指定用户密码
	 * 接口地址:/admin/updatePassword
	 * @param adminPasswordDto
	 * @return
	 */
	@PostMapping("/updatePassword")
	public R<Integer> updatePassword(@RequestBody AdminPasswordDto adminPasswordDto){
		return umsAdminService.updatePassword(adminPasswordDto);
	}
}
